// pages/supplier/withdrawal/withdrawal.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        overage:'',
        supplierId:'',
        value:''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            overage:options.amount,
            supplierId:options.supplierId
        })
    },
    handleAll(){
        let that = this
        this.setData({
            value:that.data.overage
        })
    },
    handleSubmit(){
        let that = this;
        let reg = /^(?:0\.\d{0,1}[1-9]|(?!0)\d{1,100}(?:\.\d{0,1}[1-9])?)$/;
        if(that.data.value===''){
            wx.showToast({
                icon:'none',
                title: '请输入提现金额',
            })
            return false;
        }
        if(!reg.test(that.data.value)){
            wx.showToast({
                icon:'none',
                title: '提现金额格式不正确',
            })
            return false;
        }
        if (that.data.value > that.data.overage){
            wx.showToast({
                icon:'none',
                title: '余额不足',
            })
            return false;
        }
        this.putData()
    },
    handleInput(e){
        this.setData({
            value:e.detail.value
        })
    },
    putData(){
        let that = this;
        that.setData({
            btnStatus: true
        });
        wx.showLoading({
            title: '加载中...',
        })
        util.request(api.supplier.withdrawApply,{
            applyAmount:that.data.value,
            supplierId:that.data.supplierId
        },'post').then(function(res) {
            that.setData({
                btnStatus: false
            });
            wx.showToast({
              icon: 'success',
              title:'申请成功'
            })
            wx.navigateBack({
              delta: 1,
            })
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            that.setData({
                btnStatus: false
            });
            wx.showToast({
                icon: 'success',
                title:err.message
            })
            wx.hideLoading()
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})